{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "异常代码：80131502\n",
      "异常信息：Specified argument was out of the range of valid values. (Parameter 'cancellationToken')\n",
      "异常文件:  at line: 0\n"
     ]
    }
   ],
   "source": [
    "#load \"..\\Extension\\LoadBaseTool.csx\"\n",
    "using BaseTool;\n",
    "using System.Diagnostics;\n",
    "using System.Threading;\n",
    "\n",
    "try\n",
    "{\n",
    "    Console.WriteLine(Task.FromCanceled(CancellationToken.None).Status);\n",
    "}\n",
    "catch(Exception e)\n",
    "{\n",
    "    Console.WriteLine($\"异常代码：{e.HResult:X}\");\n",
    "    Console.WriteLine($\"异常信息：{e.Message}\");\n",
    "    // 获取异常的堆栈跟踪\n",
    "    StackTrace stackTrace = new StackTrace(e, true); // true 表示捕获源文件信息\n",
    "    StackFrame frame = stackTrace.GetFrame(stackTrace.FrameCount -1); // 获取堆栈的最底部的帧\n",
    "        // 获取文件名和行号\n",
    "    string fileName = frame.GetFileName();\n",
    "    int lineNumber = frame.GetFileLineNumber();\n",
    "\n",
    "    Console.WriteLine($\"异常文件: {fileName} at line: {lineNumber}\");\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "ArgumentNullException.ThrowIfNull(\"\");\n",
    "ArgumentNullException.ThrowIfNullOrEmpty(\"\");\n",
    "ArgumentNullException.ThrowIfNullOrWhiteSpace(\"\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "ArgumentException.ThrowIfNullOrEmpty(\"\");\n",
    "ArgumentException.ThrowIfNullOrWhiteSpace(\"\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>System.ArgumentNullException: Value cannot be null. (Parameter &#39;Hello World&#39;)</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Message</td><td><div class=\"dni-plaintext\"><pre>Value cannot be null. (Parameter &#39;Hello World&#39;)</pre></div></td></tr><tr><td>ParamName</td><td><div class=\"dni-plaintext\"><pre>Hello World</pre></div></td></tr><tr><td>TargetSite</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>Data</td><td><i>(empty)</i></td></tr><tr><td>InnerException</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>HelpLink</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>Source</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>HResult</td><td><div class=\"dni-plaintext\"><pre>-2147467261</pre></div></td></tr><tr><td>StackTrace</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr></tbody></table></div></details><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ArgumentNullException _ex = new ArgumentNullException(\"Hello World\");\n",
    "_ex.Display();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Try\n",
      "Catch FormatException\n"
     ]
    },
    {
     "ename": "Error",
     "evalue": "System.FormatException: The input string 'ff' was not in a correct format.\r\n   at System.Number.ThrowFormatException[TChar](ReadOnlySpan`1 value)\r\n   at System.Int32.Parse(String s)\r\n   at Submission#13.<<Initialize>>d__0.MoveNext()\r\n--- End of stack trace from previous location ---\r\n   at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)",
     "output_type": "error",
     "traceback": [
      "System.FormatException: The input string 'ff' was not in a correct format.\r\n",
      "   at System.Number.ThrowFormatException[TChar](ReadOnlySpan`1 value)\r\n",
      "   at System.Int32.Parse(String s)\r\n",
      "   at Submission#13.<<Initialize>>d__0.MoveNext()\r\n",
      "--- End of stack trace from previous location ---\r\n",
      "   at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)"
     ]
    }
   ],
   "source": [
    "try\n",
    "{\n",
    "    Console.WriteLine($\"Try\");\n",
    "    int _ret = int.Parse(\"ff\");\n",
    "}\n",
    "catch (FormatException ex)\n",
    "{\n",
    "    Console.WriteLine($\"Catch FormatException\");\n",
    "    throw;\n",
    "}\n",
    "catch (Exception)\n",
    "{\n",
    "    Console.WriteLine($\"Catch Exception\");\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "languageName": "csharp",
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
